﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using vukhoas.src.POJO;
using System.Data.SqlClient;
using vukhoas.src;
using vukhoas.VukhoasAdmin;
using System.Web.Services;
namespace vukhoas.VukhoasAdmin
{
    public partial class ListProduct : System.Web.UI.Page
    {
        protected string sName;
        protected string sManu;
        protected string sCate;
        protected void Page_Load(object sender, EventArgs e)
        {
           
        }

        protected List<Product> lstProduct() {
            List<Product> lst = new List<Product>();
            SqlConnection conn = MyConnect.getConnection();
            conn.Open();
            try
            {
                String sql = "SELECT * FROM product WHERE name LIKE '%" + sName + "%' AND manufacture_id LIKE '%" + sManu + "%' AND category_id LIKE '%" + sCate + "%'";
                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    Product pro = new Product();
                    pro.Id = dr.GetInt32(0);
                    pro.Name = dr.GetString(1);
                    pro.Price = dr.GetString(2);
                    pro.ManufactureID = dr.GetString(3);
                    pro.CategoryID = dr.GetString(4);
                    pro.Detail = dr.GetString(5);
                    pro.Introduce = dr.GetString(6);
                    pro.Information = dr.GetString(7);
                    pro.Image = dr.GetString(8);
                    pro.CreatedDate = dr.GetDateTime(9);
                    lst.Add(pro);
                }
            }
            catch (Exception ex)
            {
                Response.Write("Error: " + ex.Message);
            }
            finally
            {
                conn.Close();
            }
            return lst;
        }

        static bool delete(int id)
        {
            bool flag = false;
            SqlConnection conn = MyConnect.getConnection();
            conn.Open();
            try
            {
                String sql = "DELETE product WHERE id="+id;
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.ExecuteNonQuery();
                flag = true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                conn.Close();
            }
            return flag;
        }

        protected void Button1_Click(object sender, CommandEventArgs e)
        {
            Alert.Show(e.CommandArgument.ToString());
        }

        [WebMethod]
        public static string del(int id) {
            if (delete(id))
                return "Xóa sản phẩm thành công";
            else
                return "Có lỗi xảy ra! Vui lòng kiểm tra lại";
        }
      
    }
}
